IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'spCdsNCreateAdmTelephoneNumber')
	BEGIN
		PRINT 'Dropping Procedure spCdsNCreateAdmTelephoneNumber'
		DROP  Procedure  spCdsNCreateAdmTelephoneNumber    
	END

GO

PRINT 'Creating Procedure spCdsNCreateAdmTelephoneNumber'
GO
/******************************************************************************
**		VSS File: $/spCdsNCreateAdmTelephoneNumber.sql 
**		Name: spCdsNCreateAdmTelephoneNumber    
**		Desc: 	This procedure creates a new record in
**				the PurVendor table.
**              
**		Return values: None
** 
**		Called by:   
**              
*******************************************************************************
**		Change History
*******************************************************************************
**		Date:		Author:				Description:
**		--------		--------				-------------------------------------------
**    
*******************************************************************************/
CREATE Procedure spCdsNCreateAdmTelephoneNumber (
	/* Param List */
	@countryCode		VARCHAR(5),
	@areaCode			VARCHAR(5),
	@number				VARCHAR(20),
	@admPersonId		INT,
	@admTelephoneTypeId	INT
)	   
AS    
	SET NOCOUNT ON

	DECLARE @_maxAdmTelephoneId			INT
	DECLARE @_startingId				INT
	DECLARE @_errorCode					INT
	
	SET @_startingId = 1
	SET @_errorCode  = 0
		
	-- Setup the IDs to be inserted by selecting the max
	-- Id of the column, and if null, start at 1.
	SET @_maxAdmTelephoneId = COALESCE (((SELECT MAX(Id) from AdmTelephoneNumber)+1), 1)
	
	-- Transaction.
	BEGIN TRANSACTION spCdsNCreateAdmTelephoneNumber

		-- Insert the AdmTelephoneNumber
		INSERT INTO AdmTelephoneNumber ( Id, 
								CountryCode, 
								AreaCode,
								Number,
								AdmPersonId,
								AdmTelephoneTypeId) 
		VALUES
							 (	@_maxAdmTelephoneId,
								@countryCode,
								@areaCode, 
								@number,
								@admPersonId,
								@admTelephoneTypeId)		
	
		SET @_errorCode = @@ERROR
	
	    IF @_errorCode <> 0 BEGIN
            ROLLBACK TRANSACTION spCdsNCreateAdmTelephoneNumber
            RAISERROR('Unable to create AdmTelephoneNumber record for AdmPerson Id: %s', 16, 1, @admPersonId)
            RETURN @_errorCode
        END

		
		-- COMMIT THE CHANGES.
		COMMIT TRANSACTION spCdsNCreateAdmTelephoneNumber

	
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
 